package com.quanshu.exchange.ui.gui.profile.order_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by jye on 2018/1/17.
 */

public class DialogDeleteOrder extends BaseDialog {

    @BindView(R.id.dialog_click_delete)
    TextView dialogClickOk;
    @BindView(R.id.dialog_click_cancel)
    TextView dialogClickCancel;
    @BindView(R.id.title)
    TextView title;
    private DeleteOrderListener deleteOrderListener;
    private int indexP;

    public BehaviorSubject getOkSubject() {
        return okSubject;
    }

    private BehaviorSubject okSubject = BehaviorSubject.create();

    public DialogDeleteOrder(@NonNull Context context, DeleteOrderListener deleteOrderListener1) {
        super(context);
        deleteOrderListener = deleteOrderListener1;
    }
    public DialogDeleteOrder(@NonNull Context context) {
        super(context);
    }
    public void show(int position) {
        super.show();
        indexP=position;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_order);
        ButterKnife.bind(this);
        initListener();
    }

    @Override
    public void init(@NonNull Context context) {
        Window window = getWindow();
        window.setWindowAnimations(R.style.BottomAnim);
        window.setGravity(Gravity.BOTTOM);
        //默认的Dialog只有5/6左右的宽度，改为全屏宽度,由dialog的布局自己来决定实际显示宽度
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(dialogClickOk, aVoid -> {

            if (deleteOrderListener!=null){
                deleteOrderListener.delete(indexP);
                dismiss();

            }else {
                okSubject.onNext(aVoid);
                cancel();
            }

        });
        RxViewHelper.getInstance().clicks(dialogClickCancel, aVoid -> dismiss());
    }

    public interface DeleteOrderListener {
        void delete(int p);
    }

}
